<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\Reservation;
use Illuminate\Http\Request;
use App\Services\Admin\Teacher;
use App\Services\Admin\Store;
use App\Http\Requests\Admin\TeacherControler\doRule;

class TeacherControler extends Controller
{
    public function index()
    {

        $result = [
            'result'=>Teacher::lists()->paginate(6),
            'select_store'=>Store::where_lists(),
            'store_id'=>\request()->get('store_id'),
            'sear_value'=>\request()->get('sear_value'),
        ];

        return view('admin.teacher.index', $result);
    }

    public function add()
    {

        $result = [
            'select_store'=>Store::where_lists(),
            'select_id' => '',
        ];

        return view('admin.teacher.edit',$result);
    }

    public function edit(Request $request, $id)
    {

        $info = Teacher::info($id);

        $result = [
            'id'=>$id,
            'select_store'=>Store::where_lists(),
            'select_id' => $info->store_id,
            'result'=> $info,
        ];

        return view( 'admin.teacher.edit', $result);

    }

    public function del($id)
    {

        $is_not_empty = Reservation::lists()->where('teacher_id',$id)->where('is_display','1')->get()->isNotEmpty();

        //if($is_not_empty)return self::fail('請先去課程表把該老師的安排取消掉再刪除!');

        Teacher::del($id);

        return self::success([]);

    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $teacher_head = !empty($request->file('teacher_head')) ? $request->file('teacher_head') : [];

            if (!empty($teacher_head)) {

                $path = $teacher_head->store('images/'. date('Ymd'));

                $input['teacher_head'] = "/upload/".$path;

            } else {
                unset($input['teacher_head']);
            }


            if (empty($input['id'])) {
                unset($input['id']);
                Teacher::add($input);
            } else {
                Teacher::edit($input['id'],$input);
            }

            return self::success([]);
        }
    }
}
